var app = {

    init:function(){
        this.bindEvent();
        this.getCurrentUser();
        var loading;
        this.param = {
            classid:"",
            pageSize:10,
            pageNo:1
        }
    },
    initTree:function(data){
        var that = this;
        // for (var i = 0; i < data.length; i++) {
        //     data[i].name = data[i].title;
        // }
        layui.tree({
            elem: '#demo' //传入元素选择器
            ,nodes: data
            ,click: function(node){
                if(node.pid == 0){
                    return;
                }
                that.param.classid = node.id;
                that.loading = layer.load(0, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                });
                that.getDisasterList();
            }  
          });
        $("#demo>li").eq(0).find("li").eq(0).addClass("active");
    },
    bindEvent:function(){
        var that = this;
        $(document).on("click","#demo li",function(e){
            if($(this).parent().hasClass("layui-tree")){
                return;
            }
            $("#demo li").removeClass("active");
            $(this).addClass("active");
            e.preventDefault();
            e.stopPropagation();
        })
        $(document).on("click",".export",function(e){
            layer.confirm('您会导出' + that.total  +'条记录，需要等待较长时间，是否确定导出？', {
                btn: ['确定','取消'] //按钮
              }, function(){
                that.exportExcel();
              }, function(){
                
              });
            e.preventDefault();
            e.stopPropagation();
        })
    },
    exportExcel:function(){
        var that = this;
        that.loading = layer.load(0, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
        $.ajax({
            url:'/api/v1/meteorology/record/exportClassRecord',
            type:'post',
            data:{classid:that.param.classid},
            success:function(res){
                layer.close(that.loading);
                if(res.errcode == "0000"){
                    layer.msg("导出成功",{icon: 1});
                    window.open(res.data.exportPath); 
                }else{
                    layer.msg(res.errmsg,{icon: 0});
                }
            },
            error:function(){

            }
        });
    },
    getCurrentUser:function(){
        var that = this;
        common.getCurrentUser().then(function(res){
            that.getTypeList();
        })
    },
    renderTable:function(data){
        var that = this;
        layui.use('table', function(){
            var table = layui.table;
            table.render({
                elem: '#table'
                //,url: '/demo/table/user/' //数据接口
                ,data: data.entities ? data.entities : []
                ,limit:data.pageSize
                ,cellMinWidth:100
                ,text: {
                    none: '暂无相关数据' //默认：无数据
                }
                ,cols: [[ //表头
                    {field: 'ly2', title: '记录来源'}
                    ,{field: 'l1', title: '古代地名'}
                    ,{field: 'l2', title: '现代地名(省份)'}
                    ,{field: 'l3', title: '现代地名(市县)'} 
                    ,{field: 'y1', title: '公元纪年'}
                    ,{field: 'y3', title: '皇帝年号'}
                    ,{field: 'content', title: '记录内容',minWidth:400}
                    ]]
                ,page: false
                ,loading: true
            });  
        });
        that.total = data.entityCount;
        layui.use(['laypage'], function(){
            var laypage = layui.laypage;
            laypage.render({
                elem: 'page'
                ,curr:data.pageNo
                ,limit:data.pageSize
                ,jump: function(obj,first){
                    if(!first){
                        if(obj.limit != that.param.pageSize){
                            that.param.pageNo = 1;
                        }else {
                            that.param.pageNo = obj.curr;
                        }
                        that.param.pageSize = obj.limit;
                        that.loading = layer.load(0, {
                            shade: [0.1,'#fff'] //0.1透明度的白色背景
                        });
                        that.getDisasterList();
                    }
                }
                ,count: data.entityCount
                ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            });
        });
    },
    getTypeList:function(){
        var that = this;
        $.ajax({
            url:'/api/v1/meteorology/record/listClass',
            type:'post',
            success:function(res){
                if(res.errcode == "0000"){
                    for (var i = 0; i < res.data.length; i++) {
                        res.data[i].name = res.data[i].title;
                    }
                    var treeNodes = common.transformTozTreeFormat(res.data);
                    if(treeNodes.length > 0){
                        treeNodes[0].spread = true;
                        if(treeNodes[0].children && treeNodes[0].children.length > 0){
                            that.param.classid = treeNodes[0].children[0].id;
                            that.initTree(treeNodes);
                            that.loading = layer.load(0, {
                                shade: [0.1,'#fff'] //0.1透明度的白色背景
                            });
                            that.getDisasterList();
                        }
                    }else{
                        layer.msg("未获取到分类数据!",{icon: 0});
                    }
                }else{
                    layer.msg(res.errmsg,{icon: 0});
                }
            },
            error:function(){

            }
        });
    },
    getDisasterList:function(){
        var that = this;
        $.ajax({
            url:'/api/v1/meteorology/record/listClassRecord',
            type:'post',
            data:this.param,
            success:function(res){
                if(res.errcode == "0000"){
                    layer.close(that.loading);
                    that.renderTable(res.data)
                }else{

                }
            },
            error:function(){

            }
        });
    }
}

app.init();